import os from os.path import isfile import glob fo=open("file.txt") for h in fo: if h.startswith("key"): print(h) print(h.find("can")) #find(str,num) starts find after that index #os.mkdir("amit") lis=os.listdir("G:\\") for i in lis: i="G:\\"+i if not isfile(i): print(i) for filename in glob.iglob('G:/*.txt', recursive=True): print(filename)